package com.szholly.pro.watergas_server.ps.android.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.ps.android.common.NonNullUtil;
import com.szholly.pro.watergas_server.ps.android.common.ValidUtils;
import com.szholly.utils.util.StringUtils;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;

public class QcParameterService {
	public static final int TYPE_LAB = 1;
	public static final int TYPE_SAMPLE = 0;
	public static final QcParameterService INSTANCE = new QcParameterService();

	private QcParameterService() {
	}

	/**
	 * 采样参数列表
	 */
	public List<RowBase> getSampleParameterList(String pointCode) throws Exception {
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("pointCode", pointCode));
		String sql = "select distinct t.MONITORPARAMCODE as \"id\",\n"
				+ "                t.MONITORPARAMNAME as \"name\",\n"
				+ "                t.MONITORPARAMUNIT as \"unit\"\n"
				+ "  from QC_COMPANYPARAMINFO t1\n"
				+ "  left join qc_monitorparam t\n"
				+ "    on t.guid = t1.paramguid\n"
				+ "  left join qc_pointinfo t2\n"
				+ "    on t2.companycode = t1.companycode\n"
				+ " where t2.pointtype = t.monitortypecode\n"
				+ "   and t.isfieldparam = '0'\n"
				+ "   and t2.pointcode = @pointCode";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		return NonNullUtil.toList(rows);
	}

	/**
	 * 项目列表--精简版 -- 用于采样界面展示
	 */
	public List<RowBase> getLabBasicParameterList(String pointCode) throws Exception {
		ValidUtils.hasText(pointCode, "站点编号为空");
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("pointCode", pointCode));
		String sql = "select distinct t1.MONITORPARAMCODE as \"id\",\n"
				+ "                t1.MONITORPARAMNAME as \"name\",\n"
				+ "                t1.MONITORPARAMUNIT as \"unit\"\n"
				+ "  from qc_monitorstationparam t\n"
				+ "  left join qc_monitorparam t1\n"
				+ "    on t1.monitorparamcode = t.paramcode\n"
				+ "   and t.monitortype = t1.monitortypecode\n"
				+ " where 1 = 1\n"
				+ "   and t1.isfieldparam = '1'\n"
				+ "   and t.stationcode = @pointCode";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		return NonNullUtil.toList(rows);
	}

	/**
	 * 项目列表--详细
	 */
	public List<RowBase> getLabParameterList(String companyCode) throws Exception {
		ValidUtils.hasText(companyCode, "公司编号为空");
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("companyCode", companyCode));
		String sql = "select t.MONITORPARAMCODE as \"id\",\n"
				+ "       t.MONITORPARAMNAME as \"name\",\n"
				+ "       t.MONITORPARAMUNIT as \"unit\",\n"
				+ "       t.RDCM             as \"errorType\",\n"
				+ "       t.monitortypecode  as \"monitorTypeCode\",\n"
				+ "       t.guid\n"
				+ "  from qc_monitorparam t\n"
				+ "  left join QC_COMPANYPARAMINFO t1\n"
				+ "    on t1.PARAMGUID = t.GUID\n"
				+ " where 1 = 1\n"
				+ "   and t1.companycode = @companyCode\n"
				+ "   and t.isfieldparam ='1'";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		Map<String, String> parameterErrorLimitDes = getParameterErrorLimitDes(companyCode);
		if (rows != null && rows.size() > 0) {
			for (RowBase rowBase : rows) {
				String parmGuid = String.valueOf(rowBase.get("GUID"));
				String errorLimitDes = parameterErrorLimitDes.get(parmGuid);
				if (!StringUtils.IsNullOrSpace(errorLimitDes)) {
					rowBase.put("errorLimitDes", errorLimitDes);
				} else {
					rowBase.put("errorLimitDes", "");
				}
				rowBase.remove("GUID");
			}
		}
		return NonNullUtil.toList(rows);
	}

	/**
	 * 项目列表--根据批次获取项目信息
	 */
	public List<RowBase> getBatchProjectList(String batchCode, String companyCode) throws Exception {
		ValidUtils.hasText(batchCode);
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("batchCode", batchCode));
		parms.add(new GlobalParameter("companyCode", companyCode));
		String sql = "select distinct t4.MONITORPARAMCODE as \"id\",\n"
				+ "                t4.MONITORPARAMNAME as \"name\",\n"
				+ "                t4.MONITORPARAMUNIT as \"unit\",\n"
				+ "                t4.RDCM             as \"errorType\",\n"
				+ "                t4.monitortypecode  as \"monitorTypeCode\",\n"
				+ "                t4.guid\n"
				+ "  from qc_sampledetaildata t\n"
				+ "  left join qc_sampledata t1\n"
				+ "    on t.samplenumber = t1.samplenumber\n"
				+ "  left join operation_task t2\n"
				+ "    on t2.task_code = t1.operation_task_code\n"
				+ "  left join qc_batch t3\n"
				+ "    on t3.id = t2.parent_task_code\n"
				+ "  left join qc_monitorparam t4\n"
				+ "    on t4.isfieldparam = '1'\n"
				+ "   and t4.monitortypecode = t1.monitortype\n"
				+ "   and t4.monitorparamcode = t.monitorparam\n"
				+ " where 1 = 1\n"
				+ "   and t.type = '1'\n"
				+ "   and t3.id = @batchCode";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		Map<String, String> parameterErrorLimitDes = getParameterErrorLimitDes(companyCode);
		if (rows != null && rows.size() > 0) {
			for (RowBase rowBase : rows) {
				String parmGuid = String.valueOf(rowBase.get("GUID"));
				String errorLimitDes = parameterErrorLimitDes.get(parmGuid);
				if (!StringUtils.IsNullOrSpace(errorLimitDes)) {
					rowBase.put("errorLimitDes", errorLimitDes);
				} else {
					rowBase.put("errorLimitDes", "");
				}
				rowBase.remove("GUID");
			}
		}
		return NonNullUtil.toList(rows);
	}

	/**
	 * 项目列表--获取该公司所有批次的项目信息
	 */
	public Map<String, List<RowBase>> getBatchProjectListMap(String companyCode) throws Exception {
		ValidUtils.hasText(companyCode, "公司编号为空");
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("companyCode", companyCode));
		String sql = "select distinct t4.MONITORPARAMCODE as \"id\",\n"
				+ "                t4.MONITORPARAMNAME as \"name\",\n"
				+ "                t4.MONITORPARAMUNIT as \"unit\",\n"
				+ "                t4.RDCM             as \"errorType\",\n"
				+ "                t4.monitortypecode  as \"monitorTypeCode\",\n"
				+ "                t4.guid,\n"
				+ "                t3.id BATCH_CODE\n"
				+ "  from qc_sampledetaildata t\n"
				+ "  left join qc_sampledata t1\n"
				+ "    on t.samplenumber = t1.samplenumber\n"
				+ "  left join operation_task t2\n"
				+ "    on t2.task_code = t1.operation_task_code\n"
				+ "  left join qc_batch t3\n"
				+ "    on t3.id = t2.parent_task_code\n"
				+ "  left join qc_monitorparam t4\n"
				+ "    on t4.isfieldparam = '1'\n"
				+ "   and t4.monitortypecode = t1.monitortype\n"
				+ "   and t4.monitorparamcode = t.monitorparam\n"
				+ " where 1 = 1\n"
				+ "   and t.type = '1'\n"
				+ "   and t3.status = '1'\n"
				+ "   and t3.company = @companyCode";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		Map<String, String> parameterErrorLimitDes = getParameterErrorLimitDes(companyCode);
		if (rows != null && rows.size() > 0) {
			for (RowBase rowBase : rows) {
				String parmGuid = String.valueOf(rowBase.get("GUID"));
				String errorLimitDes = parameterErrorLimitDes.get(parmGuid);
				if (!StringUtils.IsNullOrSpace(errorLimitDes)) {
					rowBase.put("errorLimitDes", errorLimitDes);
				} else {
					rowBase.put("errorLimitDes", "");
				}
				rowBase.remove("GUID");
			}
		}
		Map<String,List<RowBase>>resultMap = new HashMap<>();
		for (RowBase bean : rows) {
			String batchCode = bean.getSafeString("BATCH_CODE");
			if (!resultMap.containsKey(batchCode)) {
				resultMap.put(batchCode, new ArrayList<>());
			}
			bean.remove("BATCH_CODE");
			resultMap.get(batchCode).add(bean);
		}
		return resultMap;
	}

	// 获取偏差范围<参数编号，偏差范围描述> 偏差范围描述 0.23%(1~14),0.25%(15~40)
	private Map<String, String> getParameterErrorLimitDes(String companyCode) throws Exception {
		Map<String, String> resultMap = new HashMap<String, String>();
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("companyCode", companyCode));
		String sql = "select t.MCUPLIMIT, t.MCDOWNLIMIT, t.PMAD || '%' as PMAD, t.paramguid\n"
				+ "  from qc_monitorparamdetail t\n"
				+ " where 1 = 1\n"
				+ "   and t.PMAD is not null\n"
				+ "   and t.paramguid in (select distinct t.paramguid\n"
				+ "                         from QC_COMPANYPARAMINFO t\n"
				+ "                         left join qc_monitorparam t1\n"
				+ "                           on t.paramguid = t1.guid\n"
				+ "                        where t.companycode = @companyCode\n"
				+ "                          and t1.isfieldparam = '1')";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		if (rows != null && rows.size() > 0) {
			for (RowBase rowBase : rows) {
				String parameterGuid = rowBase.getSafeString("paramguid");
				String upLimit = rowBase.getSafeString("MCUPLIMIT");
				String downLimit = rowBase.getSafeString("MCDOWNLIMIT");
				String errorLimit = rowBase.getSafeString("PMAD");
				if (StringUtils.IsNullOrSpace(errorLimit)) {
					continue;
				}
				// 生产偏差描述
				String des = "±" + errorLimit + "(" + downLimit + "~" + upLimit + ")";
				if (!resultMap.containsKey(parameterGuid)) {
					resultMap.put(parameterGuid, des);
				} else {
					String preDes = resultMap.get(parameterGuid);
					resultMap.put(parameterGuid, preDes + "\n" + des);
				}
			}
		}
		return resultMap;
	}
}
